/**
 * @description 音频控件
 */
export default {
	data() {
		return {
			ac: null
		}
	},
	methods: {
		/**
		 * @description 创建控件
		 * @param {String} url 路径
		 * @param {Object} params 配置信息 
		 */
		acCreateControlsFn(url, params = {
			autoplay: false,
			playbackRate: 1,
			obeyMuteSwitch:false,
			onPlay: () => {},
			onStop: () => {},
			onPause: () => {},
			opEnded: () => {},
			onTimeUpdate: () => {}
		}) {
			this.acCreateControlsMainFn(url,params);
		},
		acCreateControlsMainFn(url, params) {
			if (this.ac) {
				this.ac.stop && this.ac.stop();
				this.ac = null;
			}
			const innerAudioContext = uni.createInnerAudioContext();
			if (params.autoplay) innerAudioContext.autoplay = params.autoplay;
			if (params.playbackRate) innerAudioContext.playbackRate = params.playbackRate;
			innerAudioContext.src = url;
			innerAudioContext.onPlay(() => {
				console.log('开始播放');
				params.onPlay && params.onPlay();
			});
			innerAudioContext.onStop(() => {
				console.log('停止播放')
				params.onStop && params.onStop();
			})
			innerAudioContext.onPause(() => {
				console.log('暂停播放')
				params.onPause && params.onPause();
			})
			innerAudioContext.onEnded(() => {
				console.log('自然播放结束事件')
				params.onEnded && params.onEnded();
			})
			innerAudioContext.onTimeUpdate(() => {
				params.onTimeUpdate && params.onTimeUpdate(this.ac.currentTime, this.ac.duration)
			})
			innerAudioContext.onError((res) => {
				this.acCreateControlsFn(url,params);
			});
			this.ac = innerAudioContext;
		}
	}

}